package order;

import java.util.Scanner;

public class order {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String[] names = new String[4];
		String[] dishMegs = new String[4];
		int[] times = new int[4];
		String[] addresses = new String[4];
		int[] states = new int[4];
		double[] sumPrices = new double[4];
		boolean isExit = false;
		int flag = 1;
		String[] dishNames = { "fish", "meat", "vegatable" };
		double[] prices = { 38.0, 20.0, 10.0 };
		int[] praiseNums = new int[3];

		names[0] = "zhangqing";
		dishMegs[0] = "fish 2";
		times[0] = 10;
		addresses[0] = "tianchen street";
		sumPrices[0] = 76.0;
		states[0] = 1;

		names[1] = "zhangqing";
		dishMegs[1] = "meat 2";
		times[1] = 18;
		addresses[1] = "tianchen street";
		sumPrices[1] = 45.0;
		states[1] = 0;

		do {
			System.out.println("welcome to use chihuolianmeng order system");

			System.out.println("******************************************");
			System.out.println("1.I want to order");
			System.out.println("2.查看餐袋");
			System.out.println("3.签收订单");
			System.out.println("4.delete order");
			System.out.println("5.praise");
			System.out.println("6.logout");
			System.out.println("******************************************");

			System.out.print("please choose:");
			int choose = in.nextInt();
			switch (choose) {
			case 1:
				System.out.println("I want to order");
				boolean isAdd = false;
				for (int j = 0; j < names.length; j++) {
					if (names[j] == null) {
						isAdd = true;
						System.out.println("please input your name:");
						String name = in.next();
						System.out.println("序号\t菜品\t单价\t点赞数");
						for (int i = 0; i < dishNames.length; i++) {
							String praiseNum = (praiseNums[i] > 0 ? praiseNums[i] + "赞" : "0");
							System.out.println(i + 1 + "\t" + dishNames[i] + "\t" + prices[i] + "\t" + praiseNum);

						}
						System.out.print("请选择菜品编号：");
						int choosedish = in.nextInt();
						System.out.print("请选择份数：");
						int num = in.nextInt();
						String dishMeg = dishNames[choosedish - 1] + num + "份";
						double sumPrice = prices[choosedish - 1] * num;
						double delPrice = (sumPrice > 50) ? 0 : 5;
						System.out.print("请输入送餐时间：");
						int time = in.nextInt();
						System.out.print("请输入地址：");
						String address = in.nextLine();

						System.out.println("订餐成功");
						System.out.println("你定的是" + dishNames[choosedish - 1] + num + "份");
						System.out.println("送餐时间：" + time + "点");
						System.out
								.println("餐费：" + sumPrice + "元，送餐费:" + delPrice + "总计：" + (sumPrice + delPrice) + "元");

						names[j] = name;
						dishMegs[j] = dishMeg;
						times[j] = time;
						addresses[j] = address;
						sumPrices[j] = sumPrice + delPrice;
						break;

					} // end if

				} // end for
				if (!isAdd) {
					System.out.println("sufficent");
				}
				break;
				
				
			case 2:
				System.out.println("查看餐袋");
				System.out.println("序号\t订餐人\t\t餐品信息\t送餐日期\t送餐地址\t\t总金额\t订单状态");
				for (int i = 0; i < names.length; i++) {
					if (names[i] != null) {
						String state = (states[i] == 0) ? "已预定" : "已完成";
						String time = times[i] + "点";
						String sumPrice = sumPrices[i] + "元";
						System.out.println(i + 1 + "\t" + names[i] + "\t" + dishMegs[i] + "\t" + time + "\t"
								+ addresses[i] + "\t" + sumPrice + "\t" + state);
					}
				}
				break;

			case 3:
				System.out.println("签收订单");
				boolean isSignFind = false;
				System.out.print("请选择要签收的订单号：");
				int signOrderId = in.nextInt();
				for (int i = 0; i < names.length; i++) {
					if (names[i] != null && states[i] == 0 && signOrderId == i + 1) {
						states[i] = 1;
						isSignFind = true;
					} else if (names[i] != null && states[i] == 1 && signOrderId == i + 1) {
						System.out.println("已签收");
						isSignFind = true;
					}
				}
				if (!isSignFind) {
					System.out.println("the order you choose doesn't exist");
				}
				break;

			case 4:
				System.out.println("delete order");
				boolean isDelFind = false;
				System.out.println("请输入要删除的订单序号：");
				int intdelId = in.nextInt();
				for (int i = 0; i < names.length; i++) {
					if (names[i] != null && states[i] == 1 && intdelId == i + 1) {
						isDelFind = true;
						for (int j = intdelId - 1; j < names.length - 1; j++) {
							names[j] = names[j + 1];
							dishMegs[j] = dishMegs[j + 1];
							times[j] = times[j + 1];
							addresses[j] = addresses[j + 1];
							states[j] = states[j + 1];
							sumPrices[j] = sumPrices[j + 1];
						}
						int endIndex = names.length - 1;
						names[endIndex] = null;
						dishMegs[endIndex] = null;
						times[endIndex] = 0;
						addresses[endIndex] = null;
						states[endIndex] = 0;
						sumPrices[endIndex] = 0;
						System.out.println("delete successfully");
						break;
					} else if (names[i] != null && states[i] == 0 && intdelId == i + 1) {
						System.out.println("你的订单未签收，不可删除");
						isDelFind = true;
						break;
					}
				}
				if (!isDelFind) {
					System.out.println("the order you want to delete doesn't exist");
				}
				break;
				
				
			case 5:
				System.out.println("praise");
				System.out.println("序号\t菜品\t\t单价\t点赞数");
				for (int i = 0; i < dishNames.length; i++) {
					String praiseNum = praiseNums[i] > 0 ? praiseNums[i] + "赞" : "0";
					System.out.println(i + 1 + "\t" + dishNames[i] + "\t\t" + prices[i] + "\t" + praiseNum);

				}
				System.out.println("请输入点赞序号：");
				int i = in.nextInt();
				praiseNums[i - 1]++;
				System.out.println("点赞成功");
				break;

			case 6:
				isExit = true;
				break;
			}
			if (!isExit) {
				System.out.println("输入0返回：");
				flag = in.nextInt();
			} else {
				break;
			}

		} while (flag == 0);
	}

}
